// pages/index/propertyDetailA/propertyDetailA.js
import { API_HOST } from "../../../utils/const"
import http from "../../../utils/http"
import util from "../../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: API_HOST,
    cid: '',
    appointmentId: '',
    isFavor: false,
    tag: '预告挂牌时效',
    detailInfo: {},
    dayGuidePriceHigh: '',
    dayGuidePriceLow: '',
    monthGuidePriceHigh: '',
    monthGuidePriceLow: '',
    yearGuidePriceHigh: '',
    yearGuidePriceLow: '',
    defaultMinPrice: '',
    defaultMaxPrice: '',
    newMaxPrice: '',
    newMinPrice: '',
    tmpUnit: '',
    showDefault: true,
    validTimeBegin: '',
    validTimeEnd: '',
    status: '',
    isShare: 'no',
    isLoginStatus: false,
    token: '',
    phoneNum: '',
    isRealUser: false,
    // testHtml: '<div class="div_class" style="line-height: 40px; color: red;">Hello&nbsp;World!<br/>dsfljsldfj</div>weurewou<br/><img src="http://img02.tooopen.com/images/20150928/tooopen_sy_143912755726.jpg" style="width:100%; height:auto;" />',
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //console.log('====propertyDetailA options.id:', options.preId,'options.isShared:', options.isShared);
    this.setData({
      cid: options.preId,
      isShare: options.isShared
    });
    //this.getInfo(options.preId);
    this.getPropertyList(options.preId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    let isLoginStatus = wx.getStorageSync("isLoginStatus") || false;
    let phoneNum = wx.getStorageSync("phoneNum") || 0;
    let token = wx.getStorageSync("token") || null;
    let isRealUser = wx.getStorageSync("isRealUser") || false;
    this.setData({
      isLoginStatus: isLoginStatus,
      token: token,
      isRealUser: isRealUser,
      showDefault: true
    })
    wx.setStorageSync("preId", this.data.cid);
    console.log('====:', this.data.isLoginStatus,this.data.token);
    this.getInfo(this.data.cid);
    //console.log('====onShow:', this.data.cid, !this.data.isShare, typeof (!this.data.isShare), typeof (this.data.isFover));
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: that.data.detailInfo.title,
      path: '/pages/index/propertyDetailA/propertyDetailA?preId=' + that.data.cid + '&isShared=yes'
    }
  },

  getInfo: function (cid){
    var that = this;
    http({
      url: "/pre/detail/" + cid,
      data: {
        //preId: cid,
      },
      method:'POST',
      success: function (res) {
        //console.log('====detail info:',res.data);
        if (!res.data) return;
        let beginTime = util.getYearMonthDay(res.data.validTimeBegin);
        let endTime = util.getYearMonthDay(res.data.validTimeEnd);
        let unit = res.data.priceUnit,
          defaultMaxPrice, defaultMinPrice;
        if (unit === '元/月') {
          defaultMaxPrice = (res.data.monthGuidePriceHigh).toFixed(0);
          defaultMinPrice = (res.data.monthGuidePriceLow).toFixed(0);
          console.log('元/月defaultMaxPrice:', defaultMaxPrice, 'defaultMinPrice:', defaultMinPrice);
        } else if (unit === '万元/年') {
          defaultMaxPrice = (res.data.yearGuidePriceHigh).toFixed(0);
          defaultMinPrice = (res.data.yearGuidePriceLow).toFixed(0);
          console.log('万元/年defaultMaxPrice:', defaultMaxPrice, 'defaultMinPrice:', defaultMinPrice);
        } else if (unit === '元/㎡/日') {
          defaultMaxPrice = (res.data.dayGuidePriceHigh).toFixed(1);
          defaultMinPrice = (res.data.dayGuidePriceLow).toFixed(1);
          console.log('元/㎡/日defaultMaxPrice:', defaultMaxPrice, 'defaultMinPrice:', defaultMinPrice);
        }

        that.setData({
          detailInfo: res.data,
          validTimeBegin: beginTime,
          validTimeEnd: endTime,
          status: res.data.attentionStatus,
          appointmentId: res.data.appointmentId,
          isFavor: !!res.data.attentionStatus,
          dayGuidePriceHigh: res.data.dayGuidePriceHigh,
          dayGuidePriceLow: res.data.dayGuidePriceLow,
          monthGuidePriceHigh: res.data.monthGuidePriceHigh,
          monthGuidePriceLow: res.data.monthGuidePriceLow,
          yearGuidePriceHigh: res.data.yearGuidePriceHigh,
          yearGuidePriceLow: res.data.yearGuidePriceLow,
          newMaxPrice: res.data.maxPrice,
          newMinPrice: res.data.minPrice,
          defaultMaxPrice: defaultMaxPrice,
          defaultMinPrice: defaultMinPrice
        });
        //console.log('####:', res.data, 'fan:', !!res.data.attentionStatus);
        console.log('####:', res.data, 'validTimeBegin:', that.data.validTimeBegin);
      },
      fail: function(res){
        console.log('====res:',res);
      }
    })
  },

  switchPrice: function(e){
    let that = this;
    let unit = e.currentTarget.dataset.unit;
    let newMaxPrice, newMinPrice;
    let dayGuidePriceHigh = that.data.dayGuidePriceHigh,
        dayGuidePriceLow = that.data.dayGuidePriceLow,
        monthGuidePriceHigh = that.data.monthGuidePriceHigh,
        monthGuidePriceLow = that.data.monthGuidePriceLow;
    if (unit === '元/月'){
      newMaxPrice = dayGuidePriceHigh !== null ? dayGuidePriceHigh.toFixed(1):'-';
      newMinPrice = dayGuidePriceLow !== null ? dayGuidePriceLow.toFixed(1) : '-';
      console.log('maxPrice', that.data.dayGuidePriceHigh, 'newMaxPrice:', newMaxPrice, 'minPrice:', that.data.dayGuidePriceLow, 'newMinPrice:', newMinPrice);
      that.setData({
        // newMaxPrice: newMaxPrice,
        // newMinPrice: newMinPrice,
        tmpUnit: '元/㎡/日',
      })
    } else if (unit === '万元/年'){
      newMaxPrice = dayGuidePriceHigh !== null ? dayGuidePriceHigh.toFixed(1) : '-';
      newMinPrice = dayGuidePriceLow !== null ? dayGuidePriceLow.toFixed(1) : '-';
      that.setData({
        // newMaxPrice: newMaxPrice,
        // newMinPrice: newMinPrice,
        tmpUnit: '元/㎡/日',
      })
    } else if (unit === '元/㎡/日'){
      newMaxPrice = monthGuidePriceHigh !== null ? monthGuidePriceHigh.toFixed(0) : '-';
      newMinPrice = monthGuidePriceLow !== null ? monthGuidePriceLow.toFixed(0) : '-';
      that.setData({
        // newMaxPrice: newMaxPrice,
        // newMinPrice: newMinPrice,
        tmpUnit: '元/月',
      })
    }
    that.setData({
      newMaxPrice: newMaxPrice,
      newMinPrice: newMinPrice,
      showDefault: !that.data.showDefault
    })
  },

  getPropertyList: function(cid){
    var that = this;
    http({
      url: "/house/queryHouseList/v1.0.0",
      data: {
        processId: cid,
        pageNum: 1,
        pageSize: 10
      },
      method:'POST',
      success: function (res) {
        console.log('list res:', res.data.rowItemList);
        if (!res) return;
        that.setData({
          list: res.data.rowItemList
        })
      },
      fail: function (res) {
        console.log('====res:', res);
      }
    })
  },

  goAppointment: function (e) {
    wx.setStorageSync("clickIsPre", true);
    let url = e.currentTarget.dataset.url;
    let that = this;
    wx.setStorageSync("preId", that.data.cid);
    if (that.data.isLoginStatus && that.data.token) {
      if (that.data.isRealUser){
        wx.navigateTo({
          url: '/pages/index/success/success?preId=' + that.data.cid,
        })
      }else{
        http({
          url: "/look/insertInfo/v1.0.0",
          method: "POST",
          data: {
            cityCode: wx.getStorageSync('cityCode') || '310100',
            preprocessId: that.data.cid
          },
          success: function (res) {
            console.log('====getUserId res:', res.code,'typeof:', res.code == '-55');
            if (res.code === '-55') {
              wx.setStorageSync("userId", res.data);
              console.log('====getStorageSync:', wx.getStorageSync('userId'));
              wx.redirectTo({
                url: '/pages/my/idCardAuth/idCardAuth?userId=' + res.data + '&phoneNum=' + wx.getStorageSync('phoneNum') + '&url=' + '/pages/index/success/success?preId=' + that.data.cid
              })
            }else{
              return false;
            } 
          }
        })
      }
    }else{
      console.log('====elseelse goAppointment:');
      wx.navigateTo({
        url: '/pages/my/login/login?url=/pages/index/propertyDetailA/propertyDetailA?preId=' + that.data.cid
      });
    }
  },

  goAppointmentsss: function(e){
    wx.setStorageSync("clickIsPre", true);
    // let that = this;
    // let url = e.currentTarget.dataset.url;
    // util.goNextPage(url);
    // that.getUserId();
    let url = e.currentTarget.dataset.url;
    let that = this;
    if (that.data.isLoginStatus && that.data.token) {
      console.log('====ifif goAppointment:');
      http({
        url: "/look/insertInfo/v1.0.0",
        method: "POST",
        data: {
          cityCode: wx.getStorageSync('cityCode') || '310100',
          preprocessId: that.data.cid
        },
        success: function (res) {
          console.log('====getUserId res:', res.code, res.data);
          if (res.code == -55) {
            wx.setStorageSync("userId", res.data);
            wx.redirectTo({
              url: '/pages/my/idCardAuth/idCardAuth?userId=' + res.data + '&phoneNum=' + wx.getStorageSync('phoneNum') + '&url=' + '/pages/index/success/success?preId=' + that.data.cid
            })
          } else {
            wx.navigateTo({
              url: '/pages/index/success/success?preId=' + that.data.cid,
            })
          }
        }
      })
    }else{
      console.log('====elseelse goAppointment:');
      wx.navigateTo({
        url: '/pages/my/login/login?url=/pages/index/propertyDetailA/propertyDetailA?preId=' + that.data.cid
      });
      //return;
    }

    //let token = wx.getStorageSync("token");
    //let that = this;
    // if (that.data.isLoginStatus && that.data.token) {
    //   console.log('====ifif goAppointment:');
    //   wx.navigateTo({
    //     url: '/pages/index/success/success?preId=' + this.data.cid,
    //   })
    // }else{
    //   console.log('====else goAppointment:');
    //   var curUrl = '/pages/index/propertyDetailA/propertyDetailA?preId=' + this.data.cid;
    //   wx.navigateTo({
    //     url: '/pages/my/login/login?redirect=' + curUrl
    //   });
    //   return;
    // }
  },

  goFinish: function(){
    wx.setStorageSync("clickIsPre", true);
    var that = this;
    //var token = wx.getStorageSync("token");
    //console.log('====token:', token);
    if (that.data.isLoginStatus && that.data.token) {
      console.log('====ifif goFinish:');
      wx.showModal({
        title: "提示",
        content: "您已经完成看房了吗？",
        cancelText: "取消",
        confirmText: "完成看房",
        success: function (res) {
          console.log('hhh', res);
          if (res.confirm) {
            that.getSuccess(that.data.appointmentId);
          } else {
            return false;
          }
        }
      });
    } else {
      console.log('====else goFinish:');
      var curUrl = '/pages/index/propertyDetailA/propertyDetailA?preId=' + this.data.cid;
      wx.navigateTo({
        url: '/pages/my/login/login?redirect=' + curUrl
      });
      return;
    }
  },
  //完成看房数据请求
  getSuccess: function (id) {
    console.log('====getSuccess:',id);
    let that = this;
    http({
      url: "/look/updateInfo/v1.0.0",
      method: "POST",
      data: {
        id: id,
        status: 1
      },
      success: function (res) {
        if (res.code == 200) {
          that.getInfo(that.data.cid);
          that.setData({
            showDefault: true
          })
        }
      }
    })
  },

  toggleLike: function (e) {console.log('====:',this.data.status);
    let that = this;
    wx.setStorageSync("clickIsPre", false);
    let token = wx.getStorageSync("token");
    if (!token) {
      let curUrl = '/pages/index/propertyDetailA/propertyDetailA?preId=' + this.data.cid;
      wx.navigateTo({
        url: '/pages/my/login/login?url=' + curUrl
      });
      return
    }
    let status = that.data.isFavor ? 0 : 1;
    //console.log('====aaa:', that.data.isFavor);
    http({
      url: "/attention/" + that.data.cid + "/" + status,
      data: {
      },
      method: "POST",
      success: function (res) {
        that.setData({
          isFavor: !that.data.isFavor
        })
        console.log('====bbb:', that.data.isFavor);
      }
    })
  },

  goHome: function(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  }
})